export default function (routes, oldPathMap) {
  let pathMap = oldPathMap || Object.create(null)
  routes.forEach(route => {
    addRouteRecord(route, pathMap)
  })
  return {
    pathMap
  }
}

// 先序深度递归
function addRouteRecord(route, pathMap, parent) {
  // parent 就是父亲
  // let path = route.path
  let path = parent ? parent.path + '/' + route.path : route.path
  let record = {
    path,
    component: route.component,
    parent
  }

  // 不能定义重复的路由
  if (!pathMap[path]) {
    pathMap[path] = record
  }

  if (route.children) {
    // 如果存在儿子，需要递归处理
    route.children.forEach(childRoute => {
      addRouteRecord(childRoute, pathMap, record) // 这个地方的父亲的记录
    })
  }
}
